<!-- <template>
  <div class="container">
    <div class="linear-gradient">
      <div class="content-wrapper">
        <div class="container-header"></div>
      </div>
    </div>
  </div>
</template> -->
<template>
  <div class="common-layout linear-gradient">
    <el-container>
      <el-header class="header-container">
        <div class="headerTitle">查询条件</div>
        <div class="header-content">
          <el-form :model="headerSearchForm" label-width="auto" class="header-search-form">
            <el-form-item label="工艺规则描述">
              <el-input v-model="headerSearchForm.productionLine" placeholder="请输入工艺规则描述" />
            </el-form-item>
            <el-form-item label="工艺规则变量">
              <el-input v-model="headerSearchForm.variableDescription" placeholder="请输入工艺规则变量" />
            </el-form-item>
            <el-form-item label="初始变量名">
              <el-input v-model="headerSearchForm.variableName" placeholder="请输入初始变量名" />
            </el-form-item>
          </el-form>
          <div class="header-buttons">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleAdd">添加</el-button>
          </div>
        </div>
      </el-header>
      <el-main>
        <div class="table-container-main">
          <el-table
            :data="tableData"
            :header-cell-style="{ background: '#eef1f6', color: '#606266', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }"
          >
            <el-table-column prop="serialNumber" label="序号">
              <template #default="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="processRuleVariable" label="工艺规则变量" />
            <el-table-column prop="processRuleDescription" label="工艺规则描述" />
            <el-table-column prop="initialVariableName" label="初始变量名" />
            <el-table-column prop="ruleEditor" label="规则编辑器" />
            <el-table-column prop="dataStart" label="数据开始" />
            <el-table-column prop="dataEnd" label="数据结束" />
            <el-table-column label="操作">
              <template #default="scope">
                <el-button type="warning" @click="handleEdit(scope.row)">修改</el-button>
                <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="pagination.total"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-main>
    </el-container>
  </div>

  <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="handleClose" ref="dialogRef" @closed="cancel">
    <el-form :model="dialogForm" label-width="auto">
      <el-form-item label="工艺规则变量">
        <el-input v-model="dialogForm.processRuleVariable" placeholder="请输入变量" />
      </el-form-item>
      <el-form-item label="工艺规则描述">
        <el-input v-model="dialogForm.processRuleDescription" placeholder="请输入工艺规则描述" />
      </el-form-item>
      <el-form-item label="初始变量名">
        <el-input v-model="dialogForm.initialVariableName" placeholder="请输入初始变量名" />
      </el-form-item>
      <el-form-item label="规则编辑器">
        <el-input v-model="dialogForm.ruleEditor" placeholder="请输入规则编辑器" />
      </el-form-item>
      <el-form-item label="数据开始">
        <el-input v-model="dialogForm.dataStart" placeholder="请输入数据开始" />
      </el-form-item>
      <el-form-item label="数据结束">
        <el-input v-model="dialogForm.dataEnd" placeholder="请输入数据结束" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="dialogFormSubmit"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts">
  import { ref, reactive, computed, onMounted, onUnmounted, nextTick, defineComponent } from 'vue';
  import { ElMessage } from 'element-plus';
  import './steelCoilServiceModelCommonStyle.less';

  interface SearchForm {
    productionLine: string;
    variableDescription: string;
    variableName: string;
  }

  export default defineComponent({
    name: 'DataDictionary',
    setup() {
      // 表单数据
      const headerSearchForm = reactive<SearchForm>({
        productionLine: '',
        variableDescription: '',
        variableName: '',
      });

      // 表格数据
      const tableData = ref([
        {
          processRuleVariable: 'c642_cpcpor1_hcpos_head_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c642_cpcpor1_hcpos',
          ruleEditor: 'max',
          dataStart: '100.0',
          dataEnd: '0.0',
        },
        {
          processRuleVariable: 'c643_cpcpor2_hcpos_head_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c643_cpcpor2_hcpos',
          ruleEditor: 'min',
          dataStart: '80.0',
          dataEnd: '10.0',
        },
        {
          processRuleVariable: 'c644_cpcpor3_hcpos_speed_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c644_cpcpor3_hcpos',
          ruleEditor: 'max',
          dataStart: '120.0',
          dataEnd: '20.0',
        },
        {
          processRuleVariable: 'c645_cpcpor4_hcpos_temp_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c645_cpcpor4_hcpos',
          ruleEditor: 'min',
          dataStart: '90.0',
          dataEnd: '5.0',
        },
        {
          processRuleVariable: 'c646_cpcpor5_hcpos_pressure_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c646_cpcpor5_hcpos',
          ruleEditor: 'max',
          dataStart: '150.0',
          dataEnd: '30.0',
        },
        {
          processRuleVariable: 'c647_cpcpor6_hcpos_level_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c647_cpcpor6_hcpos',
          ruleEditor: 'min',
          dataStart: '70.0',
          dataEnd: '15.0',
        },
        {
          processRuleVariable: 'c648_cpcpor7_hcpos_voltage_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c648_cpcpor7_hcpos',
          ruleEditor: 'max',
          dataStart: '220.0',
          dataEnd: '180.0',
        },
        {
          processRuleVariable: 'c649_cpcpor8_hcpos_current_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c649_cpcpor8_hcpos',
          ruleEditor: 'min',
          dataStart: '10.0',
          dataEnd: '2.0',
        },
        {
          processRuleVariable: 'c650_cpcpor9_hcpos_frequency_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c650_cpcpor9_hcpos',
          ruleEditor: 'max',
          dataStart: '50.0',
          dataEnd: '40.0',
        },
        {
          processRuleVariable: 'c651_cpcpor10_hcpos_weight_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c651_cpcpor10_hcpos',
          ruleEditor: 'min',
          dataStart: '85.0',
          dataEnd: '45.0',
        },
        {
          processRuleVariable: 'c652_cpcpor11_hcpos_length_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c652_cpcpor11_hcpos',
          ruleEditor: 'max',
          dataStart: '200.0',
          dataEnd: '150.0',
        },
        {
          processRuleVariable: 'c653_cpcpor12_hcpos_width_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c653_cpcpor12_hcpos',
          ruleEditor: 'min',
          dataStart: '100.0',
          dataEnd: '80.0',
        },
        {
          processRuleVariable: 'c654_cpcpor13_hcpos_height_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c654_cpcpor13_hcpos',
          ruleEditor: 'max',
          dataStart: '180.0',
          dataEnd: '120.0',
        },
        {
          processRuleVariable: 'c655_cpcpor14_hcpos_density_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c655_cpcpor14_hcpos',
          ruleEditor: 'min',
          dataStart: '2.5',
          dataEnd: '1.2',
        },
        {
          processRuleVariable: 'c656_cpcpor15_hcpos_viscosity_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c656_cpcpor15_hcpos',
          ruleEditor: 'max',
          dataStart: '500.0',
          dataEnd: '300.0',
        },
        {
          processRuleVariable: 'c657_cpcpor16_hcpos_pH_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c657_cpcpor16_hcpos',
          ruleEditor: 'min',
          dataStart: '6.0',
          dataEnd: '5.0',
        },
        {
          processRuleVariable: 'c658_cpcpor17_hcpos_light_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c658_cpcpor17_hcpos',
          ruleEditor: 'max',
          dataStart: '1000.0',
          dataEnd: '800.0',
        },
        {
          processRuleVariable: 'c659_cpcpor18_hcpos_sound_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c659_cpcpor18_hcpos',
          ruleEditor: 'min',
          dataStart: '30.0',
          dataEnd: '20.0',
        },
        {
          processRuleVariable: 'c660_cpcpor19_hcpos_vibration_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c660_cpcpor19_hcpos',
          ruleEditor: 'max',
          dataStart: '5.0',
          dataEnd: '1.0',
        },
        {
          processRuleVariable: 'c661_cpcpor20_hcpos_moisture_min',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c661_cpcpor20_hcpos',
          ruleEditor: 'min',
          dataStart: '40.0',
          dataEnd: '30.0',
        },
        {
          processRuleVariable: 'c662_cpcpor21_hcpos_time_max',
          processRuleDescription: '头部A个点的最大值',
          initialVariableName: 'c662_cpcpor21_hcpos',
          ruleEditor: 'max',
          dataStart: '3600.0',
          dataEnd: '1800.0',
        },
      ]);

      const dataSourceType = ref('');
      let dialogVisible = ref(false);
      let dialogTitle = ref('');
      const dialogForm = reactive({
        processRuleVariable: '',
        processRuleDescription: '',
        initialVariableName: '',
        ruleEditor: '',
        dataStart: '',
        dataEnd: '',
      });
      const dialogRef = ref();

      // 添加方法
      const handleAdd = () => {
        // 这里添加新增逻辑
        console.log('添加');
        dialogVisible.value = true;
        dialogTitle.value = '新增';
        Object.assign(dialogForm, {
          productionLine: '',
          areaCode: '',
          deviceCode: '',
          variableName: '',
          variableAlias: '',
          variableDescription: '',
          dataSource: '',
          triggerMark: '',
          variableAttribute: '',
          isEnabled: false,
        });
      };

      // 编辑方法
      const handleEdit = (row: any) => {
        // 这里添加编辑逻辑
        dialogVisible.value = true;
        dialogTitle.value = '修改';
        Object.assign(dialogForm, { ...row });
      };

      const cancel = () => {
        dialogVisible.value = false;
      };

      const dialogFormSubmit = () => {
        // 这里添加表单提交逻辑
        console.log('表单提交', dialogForm);
        dialogVisible.value = false;
        fetchTableData();
      };

      const handleClose = (done: () => void) => {
        ElMessageBox.confirm('Are you sure to close this dialog?')
          .then(() => {
            done();
          })
          .catch(() => {
            // catch error
          });
      };

      // 分页数据
      const pagination = reactive({
        currentPage: 1,
        pageSize: 10,
        total: tableData.value.length,
      });

      // 窗口大小变化处理
      const handleResize = () => {
        nextTick(() => {
          // 这里可以添加表格自适应高度的逻辑
        });
      };

      // 获取表格数据
      const fetchTableData = async () => {
        try {
          // 这里添加获取数据的API调用
          // const res = await api.getTableData({ ...headerSearchForm, ...pagination });
          // tableData.value = res.data;
          // pagination.total = res.total;
        } catch (error) {
          ElMessage.error('获取数据失败');
        }
      };

      // 查询方法
      const handleSearch = () => {
        pagination.currentPage = 1;
        fetchTableData();
      };

      // 重置方法
      const handleReset = () => {
        (Object.keys(headerSearchForm) as Array<keyof SearchForm>).forEach((key) => {
          headerSearchForm[key] = '';
        });
        handleSearch();
      };

      // 删除方法
      const handleDelete = (row: any) => {
        // 这里添加删除逻辑
        console.log('删除', row);
      };

      // 分页大小变化
      const handleSizeChange = (val: number) => {
        pagination.pageSize = val;
        fetchTableData();
      };

      // 页码变化
      const handleCurrentChange = (val: number) => {
        pagination.currentPage = val;
        fetchTableData();
      };

      // 生命周期钩子
      onMounted(() => {
        // fetchTableData();
        window.addEventListener('resize', handleResize);
      });

      onUnmounted(() => {
        window.removeEventListener('resize', handleResize);
      });

      return {
        headerSearchForm,
        tableData,
        pagination,
        handleSearch,
        handleReset,
        handleAdd,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        dialogVisible,
        dialogTitle,
        dialogForm,
        dialogFormSubmit,
        dialogRef,
        cancel,
        handleClose,
      };
    },
  });
</script>
